import imgui
import imgui_font
import db_connector
import card_window
import web_record_window
import web_admission_window
var db = null
using imgui

system.file.remove("./imgui.ini")
var app=window_application(get_monitor_width(0),get_monitor_height(0),"信息化办收费系统")
# fonts
var font=add_font_extend_cn(imgui_font.source_han_sans, 32)
var large_font = add_font_extend_cn(imgui_font.source_han_sans, 54)
var tiny_font = add_font_extend_cn(imgui_font.source_han_sans,18)
# accounts
var account = "马晓晨"
var password = "123456"
# window status
var if_login_window = true
var if_card_window = false
var if_login_success = false
var if_menu = false
var if_web_record_window = false
var if_web_admission_window = false
# images
var scu_image =  load_bmp_image("images/sichuan.bmp")
var cov_image = load_bmp_image("images/cov.bmp")
var ins_image = load_bmp_image("images/ins.bmp")
var back_grounds = new array
# menu
var menu_flag = {0,0,0}
# 卡片，网费提交，网费确认

# message
var pass_message = ""
#excption
var exception_string = ""
#data cache
set_font_scale(1.0) 

function login_success()
    if if_login_success
        open_popup("欢迎##popup登录") 
    end
    if begin_popup_modal("欢迎##popup登录",if_login_success,{flags.no_move,flags.always_auto_resize})
        text("登录成功! 欢迎您 " + account)
        if button("确认##confirm_download")
            if_login_success = false
        end    
        end_popup()
    end
end

function menu()
    if if_menu
        begin_window("收费系统功能界面",if_menu,{flags.no_collapse,flags.no_move,flags.no_title_bar,flags.no_resize})
            # LATER
            # set_window_size(vec2(get_monitor_width(0)/4, get_monitor_height(0)/4 * 3))
            # set_window_pos(vec2(0,get_monitor_height(0)/4))
            if menu_flag[0] == 1
                if button("卡片工本费系统")
                    if_card_window = true
                    if_web_record_window = false
                    if_web_admission_window = false
                    card_window.data.init()
                end
            end
            if menu_flag[1] == 1
                if button("网费录入")
                    if_card_window = false
                    if_web_record_window = true
                    if_web_admission_window = false
                    web_record_window.data.init()
                end
            end
            if menu_flag[2] == 1
                if button("网费确认")
                    if_card_window = false
                    if_web_record_window = false
                    if_web_admission_window = true
                    web_admission_window.data.init()
                end
            end

           
        end_window()
    end
end

function login_window() 
    if if_login_window
        var opened = true
        begin_window("信息化办收款系统登录",if_login_window,{flags.no_collapse,flags.no_resize,flags.no_move})
            var w = 580
            var h = 800
            set_window_size(vec2(w, h))
            set_window_pos(vec2((get_monitor_width(0)-w) / 2,(get_monitor_height(0)-h) / 2))
            text(" ")
            push_font(large_font)
            text("  欢迎使用信息化办收款系统")
            pop_font()
            separator()
            text(" ")
            text("                ")
            same_line()
            input_text("账户",account,10)
            text("                ")
            same_line()
            input_text_s("密码",password,30,{flags.password})
            text("                " + pass_message)
            text("                                                ")
            same_line()
            # 登录判定
            if button("登录系统##login")
                var db = db_connector.start()
                var res = db_connector.validate(account, password)
                system.out.println(res)
                if typeid res == typeid string
                    open_popup("登录异常！##popup登录异常")
                    exception_string = res
                else
                    menu_flag = res
                    if_menu = true
                    if_login_window = false
                    if_login_success = true
                    card_window.admin_name = account
                    web_admission_window.admin_name = account
                    web_record_window.admin_name = account
                    # 初始化admin姓名
                end
            end
            # LATER 图片有问题需要更新
            text(" ")
            text("                                   ") 
            same_line()
            image(scu_image,vec2(200,70))
            text(" ")
            text("                                     ") 
            same_line()
            image(cov_image,vec2(200,80))
            text(" ")
            text("                                               ") 
            same_line()
            image(ins_image,vec2(80,80))
            text("                                            ")
            same_line()  
            push_font(tiny_font)
            text("智锐科创计算机协会")
            # popups

            opened = true
            if begin_popup_modal("登录异常！##popup登录异常",opened,{flags.no_move,flags.always_auto_resize})
                text(exception_string)
                end_popup() 
            end
            pop_font()
        end_window()
    end
end

# 主要的逻辑循环
while !app.is_closed()
    app.prepare()
        style_color_dark()
        push_font(font)
        # windows
        login_window()

        menu()
        if if_card_window
            card_window.mywindow(if_card_window)
        end
        if if_web_record_window
            web_record_window.mywindow(if_web_record_window)
        end
        if if_web_admission_window
            web_admission_window.mywindow(if_web_admission_window)
        end
        login_success()
        pop_font()    
    app.render()
end
